{% extends "base.html" %}
{% block content %}
<h3><a href="{{ poll.get_absolute_url }}">{{ poll.title }}</a></h3>
<form action="." method="post">
    <ul id="poll_form">
        {{ form.as_ul }}
    </ul>
    <input type="submit" value="{% ifequal action 'create' %}Create Poll{% else %}Modify Poll{% endifequal %}" />
</form>
{% endblock %}

{% block extra %}
{% ifequal action 'modify' %}
      <div class="span-12 border">
	<h2 class="prepend-top">Choices</h2>
	<ul id="choices">
	  {% for choice in poll.choice_set.all %}
	  <li>{{ choice.choice }}</li>
	  {% endfor %}
	</ul>
	<a class="thickbox" href="{% url choice_create poll.pk %}">Add Choice</a>
      </div>
      <div class="span-12 last">
	<div class="highlight">
	  <pre><span class="k">class</span> <span class="nc">CreateChoice</span><span class="p">(</span><span class="n">AjaxFormView</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;polls/choice_create.html&quot;</span>
    <span class="n">login_required</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="n">form_class</span> <span class="o">=</span> <span class="n">CreateChoiceFormClass</span>

    <span class="k">def</span> <span class="nf">build_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">poll_id</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&#39;poll&#39;</span><span class="p">:</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Poll</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">poll_id</span><span class="p">)}</span>
    
    <span class="k">def</span> <span class="nf">initial_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&quot;initial&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&#39;poll&#39;</span><span class="p">:</span> <span class="nb">vars</span><span class="p">[</span><span class="s">&#39;poll&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">pk</span><span class="p">}}</span>

    <span class="k">def</span> <span class="nf">process_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">form</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
        
    <span class="k">def</span> <span class="nf">ajax_success</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="s">&#39;&lt;li&gt;</span><span class="si">%s</span><span class="s">&lt;/li&gt;&#39;</span><span class="o">%</span> <span class="n">result</span><span class="o">.</span><span class="n">choice</span><span class="p">)</span>
        
    <span class="k">def</span> <span class="nf">noajax_success</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">poll</span><span class="o">.</span><span class="n">get_absolute_url</span><span class="p">())</span></pre>
	</div>
      </div>
{% endifequal %}
{% endblock %}

{% block code %}
{% ifequal action 'create' %}
<span class="k">class</span> <span class="nc">CreatePoll</span><span class="p">(</span><span class="n">FormView</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;polls/poll_create.html&quot;</span>
    <span class="n">login_required</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="n">form_class</span> <span class="o">=</span> <span class="n">CreatePollFormClass</span>
    
    <span class="k">def</span> <span class="nf">initial_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="n">poll_id</span> <span class="o">=</span> <span class="nb">vars</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;poll_id&quot;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">poll_id</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">{</span><span class="s">&quot;initial&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&#39;state&#39;</span><span class="p">:</span> <span class="n">STATE_DEFAULT</span><span class="p">}}</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">{</span><span class="s">&quot;instance&quot;</span><span class="p">:</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Poll</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">poll_id</span><span class="p">)</span> <span class="p">}</span>

    <span class="k">def</span> <span class="nf">process_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="n">poll</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">commit</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
        <span class="n">poll</span><span class="o">.</span><span class="n">user</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span>
        <span class="n">poll</span><span class="o">.</span><span class="n">ip_address</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">META</span><span class="p">[</span><span class="s">&#39;REMOTE_ADDR&#39;</span><span class="p">]</span>
        <span class="n">poll</span><span class="o">.</span><span class="n">slug</span> <span class="o">=</span> <span class="n">slugify</span><span class="p">(</span><span class="n">poll</span><span class="o">.</span><span class="n">title</span><span class="p">)</span>
        <span class="n">poll</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">poll</span>
        
    <span class="k">def</span> <span class="nf">on_success</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">result</span><span class="o">.</span><span class="n">state</span> <span class="o">!=</span> <span class="n">STATE_PUBLISHED</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">reverse</span><span class="p">(</span><span class="s">&quot;poll_posted&quot;</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">reverse</span><span class="p">(</span><span class="s">&quot;poll_modify&quot;</span><span class="p">,</span> 
                 <span class="n">kwargs</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;poll_id&#39;</span><span class="p">:</span><span class="n">result</span><span class="o">.</span><span class="n">pk</span><span class="p">}))</span>
{% else %}
<span class="k">class</span> <span class="nc">ModifyPoll</span><span class="p">(</span><span class="n">FormView</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;polls/poll_create.html&quot;</span>
    <span class="n">login_required</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="n">form_class</span> <span class="o">=</span> <span class="n">ModifyPollFormClass</span>
    
    <span class="k">def</span> <span class="nf">build_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">poll_id</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&#39;poll&#39;</span><span class="p">:</span> <span class="n">get_object_or_404</span><span class="p">(</span><span class="n">Poll</span><span class="p">,</span> <span class="n">pk</span><span class="o">=</span><span class="n">poll_id</span><span class="p">)}</span>
    
    <span class="k">def</span> <span class="nf">initial_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&quot;instance&quot;</span><span class="p">:</span> <span class="nb">vars</span><span class="p">[</span><span class="s">&quot;poll&quot;</span><span class="p">]}</span>

    <span class="k">def</span> <span class="nf">process_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="n">poll</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">poll</span>
        
    <span class="k">def</span> <span class="nf">on_success</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="nb">vars</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">get_absolute_url</span><span class="p">())</span>
{% endifequal %}
{% endblock %}
